#ifndef FILESYSTEM_H
#define FILESYSTEM_H

#include <QMainWindow>
#include <QList>
#include <QFile>

#include "BlockGroup.h"

class BlockGroup;

/**
 * Singleton that represents filesystem and allows access to its statistics.
 */
class FileSystem : public QObject
{
    Q_OBJECT

	typedef QList< BlockGroup* > BlockGroupList;
	typedef BlockGroupList::const_iterator BlockGroupIter;

public:
	static FileSystem& getInstance() {
		static FileSystem fs;
		return fs;
	}

	/**
	 * Destructor. Not harmful to the data contained in the fs.
	 */
	~FileSystem();

	void startAnalysis( const QString& fileName );

	/**
	 * @return Number of free i-nodes available in this filesystem.
	 */
	int getFreeINodes() const;

	/**
	 * @return Number of free blocks available in this filesystem.
	 */
	int getFreeBlocks() const;

	/**
	 * @return Amount of free space available in filesystem in bytes.
	 */
	long getFreeSpace() const;

	/**
	 * Converts given amounts of bytes into kilobytes.
	 */
	static long ConvertToKB( long& bytes ) {
		return bytes >> 5;
	}

	/**
	 * @return Amount of blocks in filesystem.
	 */
	int getTotalBlocks() const {
		return blocksInGroup_ * groups_.count();
	}

	/**
	 * @return Amount of groups of blocks in filesystem.
	 */
	int getTotalGroups() const {
		return groups_.size();
	}

	int getTotalINodes() const {
		return iNodesInGroup_ * groups_.count();
	}

	int getBlockSize() const {
		return blockSize_;
	}

private:
	explicit FileSystem( QObject *parent = 0 );

//////////////////////////////////////////////////////////////////////
//
//		File operations section
//
//////////////////////////////////////////////////////////////////////

public:
	void readSuperblockData();

	void readGroups();


signals:
	/**
	 * Signal emited when filesystem structure analysis is completed.
	 */
	void analysisFinished();

public slots:
	void statsReady();

private:
	int blockSize_;
	int blocksInGroup_;
	int iNodesInGroup_;

	BlockGroupList groups_;

	/**
	 * File containing an ext2 data structure.
	 */
	QFile* file_;
};

#endif // FILESYSTEM_H
